package com.wunu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wunu.annotation.CurrentUser;
import com.wunu.annotation.Pass;
import com.wunu.base.Constant;
import com.wunu.base.PublicResultConstant;
import com.wunu.config.ResponseHelper;
import com.wunu.config.ResponseModel;
import com.wunu.entity.Role;
import com.wunu.entity.RoleToMenu;
import com.wunu.entity.User;
import com.wunu.model.RoleModel;
import com.wunu.service.IRoleService;
import com.wunu.util.ComUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wunu kin
 * @since 2019-03-25
 */
@CrossOrigin
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     *  角色列表
     */
    @ApiOperation(value="角色列表", notes="hander参数,需要Authorization",produces = "application/json")
    @ApiImplicitParams({
		@ApiImplicitParam(name = "pageIndex", value = "第几页", dataType = "String",paramType="query"),
		@ApiImplicitParam(name = "pageSize", value = "每页多少条", dataType = "String",paramType="query")  	
			})    
    @GetMapping("/pageList")
    //拥有超级管理员或管理员角色的用户可以访问这个接口,换成角色控制权限,改变请看MyRealm.class
    //@RequiresRoles(value = {Constant.RoleType.SYS_ASMIN_ROLE,Constant.RoleType.ADMIN},logical =  Logical.OR)
    public ResponseModel<Page<Role>> getPageList(@RequestParam(name = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                                     @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize){
        //根据姓名查分页
//        Page<Role> rolePage = roleService.selectPage(new Page<>(pageIndex, pageSize),
//                new EntityWrapper<Role>().where("role_name like {0}","%"+name+"%"));
        return ResponseHelper.buildResponseModel(roleService.selectPage(new Page<>(pageIndex, pageSize),new EntityWrapper<Role>().andNew("roleCode != {0}", 1)));
    }

    /**
     *  获取所有角色
     */
    @ApiOperation(value="获取所有角色", notes="hander参数,需要Authorization",produces = "application/json")
    @GetMapping("/all")
    public  ResponseModel<List<Role>> getAllRole(@CurrentUser User user){
    	
    	Role role = roleService.selectById(user.getRoleCode());
    	
    	if(role.getRoleType().equals(Constant.RoleType.SYS_ASMIN_ROLE)) {
    		return ResponseHelper.buildResponseModel(roleService.selectList(new EntityWrapper<Role>().eq("roleType", Constant.RoleType.ADMIN)));
    	}else {
    		return ResponseHelper.buildResponseModel(roleService.selectList(new EntityWrapper<Role>().eq("roleType", Constant.RoleType.USER)) );
		}
    	
    }

    /**
     * 获取角色详细信息
     */
    @ApiOperation(value="获取角色详细信息", notes="获取角色详细信息")
    @ApiImplicitParam(name = "roleCode", value = "role-cf8fea2055344df59a0d3e80540c78f9", required = true, dataType = "String",paramType = "path")
    @GetMapping(value = "/{roleCode}")
    public ResponseModel getById(@PathVariable("roleCode") String roleCode)throws Exception{
        return ResponseHelper.buildResponseModel(roleService.selectByRoleCode(roleCode) );
    }

    /**
     * 删除角色
     */
    @ApiOperation(value="删除角色", notes="删除角色")
    @ApiImplicitParam(name = "roleCode", value = "role-dfsg3tsdfgh55334fsdg2asdf23qrasdf3", required = true, dataType = "String",paramType = "path")
    @DeleteMapping(value = "/del/{roleCode}")
    //拥有超级管理员或管理员角色的用户可以访问这个接口,换成角色控制权限,改变请看MyRealm.class
    //@RequiresRoles(value = {Constant.RoleType.SYS_ASMIN_ROLE,Constant.RoleType.ADMIN},logical =  Logical.OR)
    public ResponseModel deleteRole(@PathVariable("roleCode") String roleCode)throws Exception{
        roleService.deleteByRoleCode(roleCode);
        return ResponseHelper.buildResponseModel(PublicResultConstant.SUCCEED);
    }
    /**
     * 添加角色
     * @param roleModel
     * @return
     */
    @ApiOperation(value="添加角色", notes="hander参数,需要Authorization",produces = "application/json")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "role", value = "{\"roleName\":\"系统管理员\"}"
                , required = true, dataType = "String",paramType="body") })
    @PostMapping(value = "/add")
    public ResponseModel add(@CurrentUser User user,@RequestBody Role role) throws Exception{
        return ResponseHelper.buildResponseModel(roleService.add(user,role));
    }
    /**
     * 修改角色信息
     */
    @ApiOperation(value="修改角色信息", notes="不需要Authorization",produces = "application/json")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "role", value = "{\"roleCode\":\"role-6bb7de09e4564fb3837fd5ab457a0e9f\",\"roleName\":\"系统管理员\"}"
                , required = true, dataType = "String",paramType="body") })
    @PostMapping(value = "/update")
    @Pass
    public ResponseModel update(@RequestBody Role role) throws Exception{
        roleService.update(role);
        return ResponseHelper.buildResponseModel(PublicResultConstant.SUCCEED);
    }
    /**
     * 添加角色菜单控制权限
     * @param roleModel
     * @return
     */
    @ApiOperation(value="添加角色菜单控制权限", notes="不需要Authorization",produces = "application/json")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "roleModel", value = "{\"menuCodes\":[{\"menuCode\":\"menu-e7c48090579444aeac20958f570d08b7\",</br>"
        											+ "\"roleCode\":\"role-6bb7de09e4564fb3837fd5ab457a0e9f\",</br>"
        											+ "\"rights\":\"[1,0,0,1]\"</br> },"
        											+ "{\"menuCode\":\"menu-b16897c1c79b45b099939f5333530eaf\",</br>"
        											+ "\"roleCode\":\"role-6bb7de09e4564fb3837fd5ab457a0e9f\",</br>"
        											+ "\"rights\":\"[1,0,1,1]\"</br> }]}"
        											, required = true, dataType = "String",paramType="body") })
    @PostMapping(value = "/addRole")
    public ResponseModel addRole(@RequestBody RoleModel roleModel) throws Exception{
        return ResponseHelper.buildResponseModel(roleService.addRoleAndPermission(roleModel));
    }

    /**
     * 修改角色权限信息
     *  String roleName;
     *  String roleCode;
     *  List<RoleToMenu> menuCodes;
     */
    @PostMapping(value = "/updateRole")
    @ApiOperation(value="修改角色权限信息", notes="HEADER传入Authorization",produces = "application/json")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "roleModel", value = "{\"menuCodes\":[{\"menuCode\":\"menu-27aadd8453de451582a1df80f3e3ed25\",</br>"
        											+ "\"roleCode\":\"role-cf8fea2055344df59a0d3e80540c78f9\",</br>"
        											+ "\"rights\":\"[1,0,0,1]\"</br> },"
        											+ "{\"menuCode\":\"role-cf8fea2055344df59a0d3e80540c78f9\",</br>"
        											+ "\"roleCode\":\"role-cf8fea2055344df59a0d3e80540c78f9\",</br>"
        											+ "\"rights\":\"[1,0,1,1]\"</br> }]}"
        											, required = true, dataType = "String",paramType="body") })
    public ResponseModel updateRole(@RequestBody RoleModel roleModel) throws Exception{
        roleService.updateRoleInfo(roleModel);
        return ResponseHelper.buildResponseModel(PublicResultConstant.SUCCEED);
    }


    /**
     * 检查角色名是不是存在
     * @param mobile
     * @return
     * @throws Exception
     */
    @ApiOperation(value="检查角色名是不是存在", notes="检查角色名是不是存在")
    @ApiImplicitParam(name = "roleName", value = "admin", required = true, dataType = "String",paramType = "query") 
    @GetMapping("/check/roleName")
    @Pass
    public ResponseModel loginByUserName(@RequestParam("roleName") String roleName) throws Exception{
    	Role role = roleService.getRoleByRoleName(roleName);
        return ResponseHelper.buildResponseModel(!ComUtil.isEmpty(role));
    }
}

